/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import Bean.Plano_acao;
import java.util.List;
import javax.persistence.EntityManager;
import Bean.Professor;
import Bean.RelatMensal;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import util.JPAUtil;

/**
 *
 * @author alexandre
 */
@Stateless(name = "RelatMensalDAO")
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class RelatMensalDAO {
    
    @PersistenceContext(unitName = "PersistenceUnit")
    private EntityManager em;

    @Resource
    private SessionContext context;//Session context que monitora se algum erro ocorre para fazer rollback
    
    public void add(RelatMensal rm){
     
        
       // EntityManager em = JPAUtil.getInstance().getEntityManager();
        em.persist(rm);
    }
    
    public void del(RelatMensal rm){
        //EntityManager em = JPAUtil.getInstance().getEntityManager();
        RelatMensal rm2 = em.merge(rm);
        em.remove(rm2);
    }
    
    public void edit(RelatMensal rm){
       // EntityManager em = JPAUtil.getInstance().getEntityManager();
        em.merge(rm);
    }
    
    public RelatMensal getRelatMensalID(int id){
       Query query = em.createQuery("select rel from RelatMensal rel where rel.id=:id");
       query.setParameter("id", id);
         return (RelatMensal)query.getSingleResult();
    
    }
    
    public List<RelatMensal> getAllRelatMensais(){
        Query query = em.createQuery("elect rel from RelatMensal rel");
        
        return query.getResultList();
    }
    
    public List<Professor> getRetornaProfPorEscola(int id){
       // return JPAUtil.getInstance().getProfessorPorEscola(Professor.class,"select prof from Professor prof where prof.escola.id=:id", id);
        Query query = em.createQuery("select prof from Professor prof where prof.escola.id=:id");
        query.setParameter("id", id);
          return query.getResultList();
        
    }
    
    public List<RelatMensal> getRel_Escola_mes(String mes, int id){
        //return JPAUtil.getInstance().getRelat_escola_mes(RelatMensal.class, "select rel from RelatMensal rel where rel.mes=:mes and rel.escola.id=:id",id, mes);
        Query query = em.createQuery("select rel from RelatMensal rel where rel.mes=:mes and rel.escola.id=:id");
        query.setParameter("mes", mes);
        query.setParameter("id", id);
        
        return query.getResultList();
    }
    
    
    public List<Plano_acao> getPlanoPorProfessor(int id){
       // return JPAUtil.getInstance().getPlanoPorprofessor(Plano_acao.class, "select plano from Plano_acao plano where plano.id=:id", id);
        Query query = em.createQuery("select plano from Plano_acao plano where plano.id=:id");
        query.setParameter("id", id);
        return query.getResultList();
    }
    
     public List<RelatMensal> getRelatAllEscola(String mes){
        //return JPAUtil.getInstance().getRelat_escola_mes(RelatMensal.class, "select rel from RelatMensal rel where rel.mes=:mes and rel.escola.id=:id",id, mes);
        Query query = em.createQuery("select rel from RelatMensal rel   where rel.mes=:mes group by rel.escola.escola,rel.id ");
        query.setParameter("mes", mes);
        
        
        return query.getResultList();
    }
     
     public List<RelatMensal> getRelatEscolaProfMes(String mes,int prof_id,int esc_id){
        //return JPAUtil.getInstance().getRelat_escola_mes(RelatMensal.class, "select rel from RelatMensal rel where rel.mes=:mes and rel.escola.id=:id",id, mes);
        Query query = em.createQuery("select rel from RelatMensal rel   where rel.mes=:mes and rel.professor.id=:prof_id and rel.escola.id=:esc_id ");
        query.setParameter("mes", mes);
        query.setParameter("prof_id", prof_id);
        query.setParameter("esc_id",esc_id);
        
        
        return query.getResultList();
    }
     
     
    
}
